package com.talios.jira.settings.projects;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.ui.DialogBuilder;
import com.intellij.openapi.ui.DialogWrapper;
import com.talios.jira.JiraException;
import com.talios.jira.browser.JiraListComboBoxModel;
import com.talios.jira.browser.JiraProjectDetails;
import com.talios.jira.rpc.JiraRpcClient;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

public class RPCProjectSettings implements ProjectSettings {
    private static final Logger LOGGER = Logger.getInstance("Jira Browser");

    private JButton cancel;
    private JButton apply;
    private String componentId;
    private String componentName;
    private JPanel mainPanel;

    private boolean changed;
    private JiraProjectDetails project;
    private DialogWrapper dialog;
    private JiraRpcClient rpcClient;
    private JComboBox projectCombo;
    private JComboBox componentCombo;
    private JiraListComboBoxModel projectModel;
    private JiraListComboBoxModel componentModel;
    private String title;

    public RPCProjectSettings(final JiraProjectDetails project, JiraRpcClient rpcClient, String title) throws HeadlessException {
        this.project = project;
        this.rpcClient = rpcClient;
        this.title = title;

        apply.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                storeValuesTo(project);
                dialog.close(0);
            }
        });

        cancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dialog.close(-1);
            }
        });

        projectCombo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // Project change, load new component projectModel
                Object selectedItem = projectCombo.getSelectedItem();
                if (selectedItem != null) {
                    String projectKey = projectModel.getKeyOfName(selectedItem);
                    setComponent(projectKey, "-1");
                }
            }
        });

        componentCombo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Object selectedItem = componentCombo.getSelectedItem();
                if (selectedItem != null) {
                    componentId = componentModel.getIdOfSelectedItem();
                    componentName = (String) componentCombo.getSelectedItem();
                }
            }
        });
    }

    public boolean editProject(DialogWrapper owner) {

        DialogBuilder db = new DialogBuilder(owner.getContentPane());

        dialog = db.getDialogWrapper();

        dialog.setSize(350, 125);
        dialog.setTitle(title);
        dialog.setModal(true);
        dialog.getContentPane().add(mainPanel);

        changed = false;

        loadValuesFrom(project);

        dialog.show();

        return changed;
    }

    private void storeValuesTo(JiraProjectDetails project) {

        String projectId = projectModel.getIdOfName(projectCombo.getSelectedItem());
        String projectKey = projectModel.getKeyOfName(projectCombo.getSelectedItem());

        changed = !projectId.equals(project.getProjectId()) || !componentId.equals(project.getComponentId());

        project.setProjectId(projectId);
        project.setProjectName((String) projectCombo.getSelectedItem());
        project.setProjectKey(projectKey);

        project.setComponentId(componentId);
        project.setComponentName(componentName);
    }

    private void loadValuesFrom(JiraProjectDetails project) {
        componentId = project.getComponentId();

        setProject(project);

    }

    private void setProject(JiraProjectDetails project) {
        List<? extends Object> projectList = new ArrayList<Object>();
        try {
            projectList = rpcClient.getProjects();
        } catch (JiraException e) {
            LOGGER.error("Unable to get project list: " + e.getMessage(), e);
        }

        projectModel = new JiraListComboBoxModel(projectList);
        projectCombo.setModel(projectModel);

        int index = projectModel.getIndexOfId(project.getProjectId());
        projectCombo.setSelectedIndex(index);

        setComponent(project.getProjectKey(), project.getComponentId());
    }

    /**
     * @jira INTPLUG-66 Need an option to disable annotation
     */
    private void setComponent(String projectKey, String componentId) {
        List<Hashtable<String, String>> componentList = new ArrayList<Hashtable<String, String>>();
        Hashtable<String, String> noComponent = new Hashtable<String, String>();
        noComponent.put("id", "-1");
        noComponent.put("name", "All Components");
        componentList.add(noComponent);

        try {
            if (projectKey != null) {
                List<Hashtable<String, String>> projectComponentList = rpcClient.getComponents(projectKey);
                componentList.addAll(projectComponentList);
            }
        } catch (JiraException e) {
            LOGGER.error("Unable to get components for project: " + e.getMessage(), e);
        }

        // TODO @jira INTPLUG-96 Should cache component list along with versions/unreleased versions.
        componentModel = new JiraListComboBoxModel(componentList);
        componentCombo.setModel(componentModel);

        componentCombo.setSelectedIndex(componentModel.getIndexOfId(componentId));
    }

}
